package com.ylyq.ylxx.quarz;

import org.quartz.*;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @ClassName JobServiceImpl
 * @Description TODO
 * @Author xiaweichao
 * @Date 2020/7/3 11:01
 * @Version 1.0
 */
@Service
public class JobServiceImpl {
    @Autowired
    private Scheduler scheduler;

    /**
     * 删除job
     *
     * @param triggerName  触发器名称
     * @param triggerGroup 触发器分组
     * @param jobName      任务名称
     * @param jobGroup     任务分组
     * @throws SchedulerException
     */
    public void deleteJob(String triggerName, String triggerGroup, String jobName, String jobGroup) throws SchedulerException {
        TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup);
        scheduler.pauseTrigger(triggerKey);
        scheduler.unscheduleJob(triggerKey);
        JobKey jobKey = JobKey.jobKey(jobName, jobGroup);
        scheduler.deleteJob(jobKey);
    }

    public void deleteJobByGroup(String jobGroup,String triggerGroup) throws SchedulerException {
        GroupMatcher<TriggerKey> triggerKeyGroupMatcher=GroupMatcher.triggerGroupEquals(triggerGroup);
        Set<TriggerKey> triggerKeys = scheduler.getTriggerKeys(triggerKeyGroupMatcher);
        List<TriggerKey> triggerkeyList = new ArrayList<TriggerKey>();
        triggerkeyList.addAll(triggerKeys);
        scheduler.unscheduleJobs(triggerkeyList);
        scheduler.pauseTriggers(triggerKeyGroupMatcher);

        GroupMatcher<JobKey> matcher = GroupMatcher.groupEquals(jobGroup);
        Set<JobKey> jobkeySet = scheduler.getJobKeys(matcher);
        List<JobKey> jobkeyList = new ArrayList<JobKey>();
        jobkeyList.addAll(jobkeySet);
        scheduler.deleteJobs(jobkeyList);
    }


    /**
     * 修改定时任务
     *
     * @param oldTriggerKey 需要修改的TriggerKey 也就是唯一标识
     * @param cron          新的cron表达式
     */
    public void updateJob(TriggerKey oldTriggerKey, String cron) {
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity(oldTriggerKey).withSchedule(scheduleBuilder).build();
        try {
            scheduler.rescheduleJob(oldTriggerKey, cronTrigger);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    /**
     * 新增job任务
     *
     * @param jobName          job名称
     * @param jobGroupName     job分组名称
     * @param triggerName      触发器名称
     * @param triggerGroupName 触发器分组名称
     * @param jobClass         需要执行的job.class
     * @param cron             cron 表达式
     * @throws SchedulerException
     */
    public void addJob(String jobName, String jobGroupName,
                       String triggerName, String triggerGroupName, JobDataMap jobDataMap, Class jobClass, String cron) throws SchedulerException {
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
        JobDetail jobDetail = null;
        Trigger trigger = null;
        if (jobDataMap == null) {
            jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();
            trigger = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroupName).startNow()
                    .withSchedule(cronScheduleBuilder).build();
        } else {
            jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).usingJobData(jobDataMap).build();
            trigger = TriggerBuilder.newTrigger().withIdentity(triggerName, triggerGroupName).usingJobData(jobDataMap).startNow()
                    .withSchedule(cronScheduleBuilder).build();
        }
        scheduler.scheduleJob(jobDetail, trigger);
    }
}
